package me.numark.providers;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by tian on 14-9-18:上午7:53.
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int DB_VERSION = 1;

    public DatabaseHelper(Context context, String dbName) {
        super(context, dbName, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.beginTransaction();
        createTables(db);
        db.setTransactionSuccessful();
        db.endTransaction();
    }

    private void createTables(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS "
                + Numa.NewsColumns.HOT_NEWS_TABLE_NAME + " ( "
                + Numa.NewsColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + Numa.NewsColumns.NEWS_ID + " INTEGER NOT NULL, "
                + Numa.NewsColumns.FROM_STR + " TEXT, "
                + Numa.NewsColumns.TITLE + " TEXT, "
                + Numa.NewsColumns.SUBTITLE + " TEXT, "
                + Numa.NewsColumns.CREATE + " TEXT, "
                + Numa.NewsColumns.FROM_TO + " TEXT, "
                + Numa.NewsColumns.FACE_IMAGE + " TEXT, "
                + Numa.NewsColumns.CREATE_TIMESTAMP + " INTEGER, "
                + Numa.NewsColumns.HOT + " INTEGER, "
                + Numa.NewsColumns.TYPE + " INTEGER, "
                + Numa.NewsColumns.JSON + " TEXT, "
                + Numa.NewsColumns.CHANNEL + " INTEGER, "
                + Numa.NewsColumns.DAY + " TEXT, "
                + "UNIQUE (" + Numa.NewsColumns.NEWS_ID + ", " + Numa.NewsColumns.CHANNEL + ")"
                + ");");

        db.execSQL("CREATE TABLE IF NOT EXISTS "
                + Numa.FeedColumns.TABLE_NAME + " ("
                + Numa.FeedColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + Numa.FeedColumns.FEED_ID + " INTEGER NOT NULL, "
                + Numa.FeedColumns.FEED_TIME + " INTEGER, "
                + Numa.FeedColumns.CONTENT + " TEXT, "
                + Numa.FeedColumns.FEED_TYPE + " TEXT, "
                + Numa.FeedColumns.CREATE_AT + " TEXT, "
                + " UNIQUE (" + Numa.FeedColumns.FEED_ID + ", " + Numa.FeedColumns.FEED_TYPE + ")"
                + ");");

        db.execSQL("CREATE TABLE IF NOT EXISTS nm_dialogs (_id INTEGER PRIMARY KEY,"
                + " uid INTEGER NOT NULL, nick TEXT, pic TEXT, last_text TEXT, "
                + "last_time INTEGER, badge INTEGER, fullLoaded BOOL);");
        db.execSQL("INSERT OR REPLACE INTO nm_dialogs (_id, nick, uid, last_time) VALUES (0, '评论', 0, 9023372036854775807);");
        db.execSQL("CREATE TABLE IF NOT EXISTS nm_messages "
                + "(_id INTEGER PRIMARY KEY, did INTEGER NOT NULL, "
                + "uid INTEGER NOT NULL, text TEXT, hash TEXT, time INTEGER, UNIQUE(uid, hash));");
        db.execSQL("CREATE TABLE IF NOT EXISTS nm_users "
                + "(_id INTEGER PRIMARY KEY, my_follows BOOL DEFAULT 0, my_fans BOOL DEFAULT 0, pic TEXT, pinyin TEXT, nick TEXT, des TEXT, fans INTEGER, follows INTEGER)");
        db.execSQL("CREATE INDEX IF NOT EXISTS nm_users_py ON nm_users(pinyin);");
        db.execSQL("CREATE TABLE IF NOT EXISTS nm_kvs (_id INTEGER PRIMARY KEY, s_int INTEGER, s_des TEXT)");
        db.execSQL("INSERT OR REPLACE INTO nm_kvs (_id, s_des, s_int) VALUES (0, 'max message time', 0);");
        db.execSQL("INSERT OR REPLACE INTO nm_kvs (_id, s_des, s_int) VALUES (1, 'max message id', 0);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
